using System;
using XiheNet;

namespace Com.Common;

public static class Utils
{
    /// <summary>
    /// 加密函数
    /// </summary>
    /// <param name="pwd">密码</param>
    /// <param name="salt">盐</param>
    /// <returns>加密后的密码</returns>
    public static string Encrypt(string pwd, string salt) {
        string pwd1 = XUtils.Md5Encryp(pwd);
        return XUtils.Md5Encryp($"{salt}{pwd1}{salt}");
    }

    /// <summary>
    /// 对字符串进行掩码处理
    /// </summary>
    /// <param name="input">原始字符串</param>
    /// <param name="startLen">开头保留的字符数</param>
    /// <param name="endLen">结尾保留的字符数</param>
    /// <param name="maskChar">掩码字符（默认 '*'）</param>
    /// <returns>掩码处理后的字符串</returns>
    public static string MaskString(string input, int startLen, int endLen, char maskChar = '*')
    {
        if (string.IsNullOrEmpty(input)) 
            return input;
        
        // 处理边界情况
        startLen = Math.Max(0, startLen);
        endLen = Math.Max(0, endLen);
        
        int totalLength = input.Length;
        
        // 当字符串过短时的处理
        if (totalLength <= startLen + endLen)
        {
            // 保留开头 + 全部掩码
            if (startLen > 0 && totalLength > startLen)
                return input.Substring(0, startLen) + new string(maskChar, totalLength - startLen);
            
            // 保留结尾 + 全部掩码
            if (endLen > 0 && totalLength > endLen)
                return new string(maskChar, totalLength - endLen) + input.Substring(totalLength - endLen);
            
            // 全部掩码
            return new string(maskChar, totalLength);
        }
        
        // 正常处理流程
        string start = input.Substring(0, startLen);
        string end = input.Substring(totalLength - endLen);
        return start + new string(maskChar, totalLength - startLen - endLen) + end;
    }

}
